package compiler.nonterminals;

import howl.types.Report;
import compiler.HowlToken;
import compiler.HowlType;
import compiler.TokenReader;
import compiler.exceptions.HowlParserException;
import compiler.terminals.BooleanExpression;
import compiler.terminals.FeedName;
import compiler.terminals.Frequency;

public class ReportStmt extends HowlToken {

	private FeedName feedname;

	private BooleanExpression exp;

	private Integer top;

	private Frequency freq;

	public ReportStmt(TokenReader tokenReader) {
		super(tokenReader);
	}


	@Override
	public String codeGenerate() {
		String str = "Report " + this.feedname.codeGenerate() + " ";
		if (top != null) {
			str += this.top.toString() + " most recent";
		} else {
			str += this.freq.codeGenerate() + " " + this.exp.codeGenerate();

		}
		return str;
	}

	@Override
	public HowlToken parse() throws HowlParserException {
		String command = this.tokenReader.consumeToken();
		if (!"report".equals(command))
			throw new HowlParserException(this.tokenReader.getStatement(),
					this.tokenReader.getCharNo());

		this.feedname = (FeedName) new FeedName(this.tokenReader).parse();

		try {
			String tmp = this.tokenReader.nextToken();
			this.top = Integer.parseInt(tmp);
			this.tokenReader.consumeToken();
			// Consume "most recent"
			this.tokenReader.consumeToken();
			this.tokenReader.consumeToken();

		} catch (NumberFormatException e) {
			String tmp = this.tokenReader.nextToken();
			if (tmp.equals("stop")) {

			} else {

				try {
					this.freq = (Frequency) new Frequency(this.tokenReader)
							.parse();
					this.exp = (BooleanExpression) new BooleanExpression(
							this.tokenReader).parse();
				} catch (Exception e1) {

				}
			}
		}

		return this;
	}


	/**
	 * @return the feedname
	 */
	public FeedName getFeedname() {
		return feedname;
	}


	/**
	 * @param feedname the feedname to set
	 */
	public void setFeedname(FeedName feedname) {
		this.feedname = feedname;
	}


	/**
	 * @return the exp
	 */
	public BooleanExpression getExp() {
		return exp;
	}


	/**
	 * @param exp the exp to set
	 */
	public void setExp(BooleanExpression exp) {
		this.exp = exp;
	}


	/**
	 * @return the top
	 */
	public Integer getTop() {
		return top;
	}


	/**
	 * @param top the top to set
	 */
	public void setTop(Integer top) {
		this.top = top;
	}


	/**
	 * @return the freq
	 */
	public Frequency getFreq() {
		return freq;
	}


	/**
	 * @param freq the freq to set
	 */
	public void setFreq(Frequency freq) {
		this.freq = freq;
	}


}
